/*
Given a set of distinct integers, S, return all possible subsets.

Note:
Elements in a subset must be in non-descending order.
The solution set must not contain duplicate subsets.
For example,
If S = [1,2,3], a solution is:

[
  [3],
  [1],
  [2],
  [1,2,3],
  [1,3],
  [2,3],
  [1,2],
  []
]
*/

class Solution {
public:
    vector<vector<int> > subsets(vector<int> &S) {
        vector<vector<int> > res;
        vector<int> resone;
        sort(S.begin(), S.end());
        do_subsets(resone, res, 0, S);
        return res;
    }
private:
    void do_subsets(vector<int> &resone, vector<vector<int> >&res, int start, vector<int> &numbers) {
        // push current result
        res.push_back(resone);
        for (int i = start; i < numbers.size(); i++) {
            resone.push_back(numbers[i]);
            do_subsets(resone, res, i+1, numbers);
            resone.pop_back();
        }
    }
};
